<template>
  <div class="menu-wrapper" v-if="!item.hidden">
    <template v-if="typeof(item.children) === 'undefined' || item.children.length === 0">
      <span v-on:click="go(resolvePath(item.path))">
        <el-menu-item :index="resolvePath(item.path)" :class="{'submenu-title-noDropdown':!isNest}">
          <svg-icon v-if="item.meta&&item.meta.icon" :icon-class="item.meta.icon"></svg-icon>
          <span v-if="item.meta&&item.meta.title">{{item.meta.title}}</span>
        </el-menu-item>
      </span>
    </template>
    <template v-else>
      <el-sub-menu :index="item.name||item.path">
        <template v-slot:title>
          <svg-icon v-if="item.meta&&item.meta.icon" :icon-class="item.meta.icon"></svg-icon>
          <span v-if="item.meta&&item.meta.title">{{item.meta.title}}</span>
        </template>

        <template v-for="(child, index) in item.children">
          <template  v-if="!child.hidden">
            <template v-if="child.children&&child.children.length>0">
              <sidebar-item-link :is-nest="true" class="nest-menu" :item="child" :key="child.path" :base-path="resolvePath(child.path)"></sidebar-item-link>
            </template>
            <template v-else>
              <span v-on:click="go(resolvePath(child.path))" :key="index">
                <el-menu-item :index="resolvePath(child.path)">
                  <svg-icon v-if="child.meta&&child.meta.icon" :icon-class="child.meta.icon"></svg-icon>
                  <span v-if="child.meta&&child.meta.title">{{child.meta.title}}</span>
                </el-menu-item>
              </span>
            </template>
          </template>
        </template>
      </el-sub-menu>
    </template>
  </div>
</template>

<script>
import { checkLogin } from '@/api/login'
import { mapGetters } from 'vuex'
import path from 'path'

export default {
  name: 'SidebarItemLink',
  computed: {
    ...mapGetters([
      'token'
    ])
  },
  props: {
    // route配置json
    item: {
      type: Object,
      required: true
    },
    isNest: {
      type: Boolean,
      default: false
    },
    basePath: {
      type: String,
      default: ''
    },
    baseUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      onlyOneChild: null
    }
  },
  created() {
  },
  methods: {
    resolvePath(...paths) {
      return this.baseUrl + path.resolve(this.basePath, ...paths)
    },
    go(url) {
      return checkLogin().then(response => {
        // console.log(this.$router)
        // console.log(this.$router.replace(location))
        // 更新vuex中iframeHref的状态
        url += '?accessType=iframe&token=' + this.token
        console.log('-----------click menu open iframe, iframe src is:-----------')
        console.log(url)
        this.$store.dispatch('SetIframeHref', url).then(() => {
        }).catch(() => {
        })
      }).catch(err => {
        err
      })
    }
  }
}
</script>
